package com.easy.frame.servers.sys.dataauth;

import com.baomidou.mybatisplus.core.injector.ISqlInjector;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.easy.frame.base.service.mp.dataauth.component.DataAuthCacheHelper;
import com.easy.frame.base.service.mp.dataauth.component.DataAuthInterceptor;
import com.easy.frame.base.service.mp.dataauth.component.DataAuthSqlInjector;
import com.easy.frame.servers.sys.entity.SysFile;
import com.easy.frame.servers.sys.entity.SysStaff;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;

/**
 * @author zzf
 * @date 2021/2/19 15:55
 */
@Slf4j
@Configuration
public class SqlHandlerConfig {

    @Bean
    ISqlInjector sqlInjector() {
        return new DataAuthSqlInjector();
    }


    @Bean
    @Order
    MybatisPlusInterceptor myRoleDataAuthSqlHandler() {
        MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();
        DataAuthInterceptor dataAuthInterceptor = new DataAuthInterceptor(new MyDataAuthStrategy());

        mybatisPlusInterceptor.addInnerInterceptor(dataAuthInterceptor);
        return mybatisPlusInterceptor;
    }

    @Bean
    @Order
    SysFile roleDataAuthSqlHandler() {

        DataAuthCacheHelper builder = new DataAuthCacheHelper()
                // TODO finish bind
                .setSqlHandler(new MyRoleDataAuthSqlHandler())
                .addTargetEntityClass(SysStaff.class)
                .setTargetFieldName(SysStaff::getTenantId)
                .bind();
        builder.cache();
        return new SysFile();
    }


}
